define(function(require,exports,module){
	
	require('common');
    //检测类
    var Auth = require('qd-auth-1.0');

	var tpl = require('../../tpl/member/redpacket_availableRedEnvelope.tpl');//载入tpl模板
	var curId = $("#myRedpacket");
    var pagerid = "kkpagerRedpacket";
    var curChoiceRedPacket;
    var _page_token = $("#_page_token").html();
    var linkExchange = null; // 选择的兑换链接换件

    // 兑换
    $("#exchange").live("click",function(){
         linkExchange = $(this);
    	 curChoiceRedPacket = $(this).parent().parent().find("#choiceRedPacket").val();
	          //构造确认框DOM
	      window.layer.open({
	        type: 1,
	        closeBtn: 0,
	          title: "温馨提示",
	          area: ['450px', '190px'],
	          border: [1, 1, '#cecfd0'],
	          content: "<div class='tipsWrap w450'>"+"<div class='tipsTxt'>"+"<i class='iconfont tipIco'>"+"&#xe63c;"+"</i><span>"+"是否兑换红包？"+"</span></div>"+"<div class='tipsBtnBar'>"+"<a href='javascript:' class='okBtn' id='true'>"+"确定"+"</a>"+"<a href='javascript:' class='okBtn' id='goback'>"+"返回"+"</a></div></div>"
	      });
	});

    $("#goback").live("click",function(){
        layer.closeAll();
    });

    // OK进行兑换
    $("#true").live("click",function(){
        linkExchange.closest("td").html("兑换中");
        layer.closeAll();
        var postData = {};
        postData.RedEnvelopeIds = curChoiceRedPacket;
        postData._page_token = _page_token || "";
        $.ajax({
            type: "post",
            url: "/member/redpacket/redEnvelopeExchange.html",
            dataType: "json",
            data:postData,
            success: function(data){
                if(data.result == false){
                    $.showWarnDialogWithClose(data.msg,function(){
                        // 刷新
                        window.location.reload();
                    },{area: ['470px', '210px']});
                } else {
                    //构造确认框DOM
                    window.layer.open({
                        type: 1,
                        closeBtn: 1,
                        title: "&nbsp;",
                        area: ['450px', '190px'],
                        border: [1, 1, '#cecfd0'],
                        content: getHtml()
                    });
                    function getHtml(){
                        var html = "<div class='tipsWrap w450'>"+"<div class='tipsTxt'>"+"<i class='iconfont tipIco'>"+"&#xe63c;"+"</i><span>"+data.msg+"</span></div>"+"<div class='tipsBtnBar'>{link}</div></div>"
                        var link = "<a href='javascript:' onclick='javascript:window.location.href=window.location.href;' class='okBtn'>返回</a>";
                        if(!data.result)
                        {
                            link =  "<a href='/member/security/setting.html' class='okBtn'>立即开户</a>";
                        }
                        return html.replace("{link}",link);
                    }
                }
            }
        });
    });

	//初始化显示表格、搜索
	require.async(['./showTable','./search'],function(showTable,search){
		showTable.ajaxUrl('/member/redpacket/availableRedEnvelopeJSON.html?status='+99,tpl,curId,pagerid);
		search.search('/member/redpacket/availableRedEnvelopeJSON.html?',tpl,curId,pagerid);
        $(".my_redpacket").click(function(){
            showTable.ajaxUrl('/member/redpacket/availableRedEnvelopeJSON.html?status='+99,tpl,curId,pagerid);
        });
	});

	$(".my_preferences").tabChange({
		isClick:true,
		isHover:false,
		childLi:".title_list li",//tab选项卡
		childContent:".list_div",//tab内容
		hoverClassName:"hover",//选中当前选项卡的样式
		callBack:false
	});

	function getQueryString(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) return unescape(r[2]); return null;
	}

	var tab=getQueryString("tab");
	if(tab=="4") {
		$("#myMovieTicketDiv").show();
		$(".title_list").children().removeClass("hover");
		$(".title_list").children().eq(3).addClass("hover");
		$("#myRedpacket").hide();
		var mtpl = require('../../tpl/member/movieTicket.tpl');//载入tpl模板
		var mcurId = $("#myMovieTicketDiv");
		var mpagerid = "kkpagerMovieTicket";
		//初始化显示表格、搜索
		require.async(['./showTable','./search'],function(showTable,search){
			search.search('/movieTicket/myMovieTicketList.html?',mtpl,mcurId,mpagerid);
			showTable.ajaxUrl('/movieTicket/myMovieTicketList.html?page=1&status=' + 99,mtpl,mcurId,mpagerid);
		});
	}
    if(tab=="3") {
        $("#myUprateDiv").show();
        $(".title_list").children().removeClass("hover");
        $(".title_list").children().eq(2).addClass("hover");
        $("#myRedpacket").hide();
        var ctpl = require('../../tpl/member/coupon.tpl');//载入tpl模板
        var ccurId = $("#myUprateDiv");
        var cpagerid = "kkpagerUprate";
        //初始化显示表格、搜索
        require.async(['./showTable','./search'],function(showTable,search){
            search.search('/uprate/myRateInterestList.html?',ctpl,ccurId,cpagerid);
            showTable.ajaxUrl('/uprate/myRateInterestList.html?status=' + 99,ctpl,ccurId,cpagerid);
        });
    }
    if(tab=="6") {
        $("#myEnjoyGoodsDiv").show();
        $(".title_list").children().removeClass("hover");
        $(".title_list").children().eq(5).addClass("hover");
        $("#myRedpacket").hide();
        var etpl = require('../../tpl/member/enjoyUser.tpl');//载入tpl模板
        var ecurId = $("#myEnjoyGoodsDiv");
        var epagerid = "kkpagerEnjoyUser";
        //初始化显示表格、搜索
        require.async(['./showTable','./search'],function(showTable,search){
            search.search('/enjoyUser/list.html?',etpl,ecurId,epagerid);
            showTable.ajaxUrl('/enjoyUser/list.html?',etpl,ecurId,epagerid);
        });
    }
});